//
//  TFStatusRetweetView.swift
//  weiboSina
//
//  Created by admin on 16/1/4.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SnapKit
class TFStatusRetweetView: UIView {

    /**
     记录当前控件的底部约束
     */
    var bottomConstraint: Constraint?
    
    var statusViewModel: TFStatusViewModel? {
    
        didSet{
        contentLabel.text = statusViewModel?.retweetStatusText
            
            //卸载掉原来的约束
            bottomConstraint?.uninstall()
            if let picurls = statusViewModel?.status?.retweeted_status?.pic_urls where picurls.count > 0 {
            
                statusPictureView.hidden = false
                /**
                设置配图要显示的数据
                */
                statusPictureView.picUrls = picurls
                //更新约束
              self.snp_updateConstraints(closure: { (make) -> Void in
                
                bottomConstraint = make.bottom.equalTo(statusPictureView.snp_bottom).offset(TFStatusCellMargin).constraint
              })
                
            }else{
            
            statusPictureView.hidden = true
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    bottomConstraint = make.bottom.equalTo(contentLabel
                    .snp_bottom).offset(TFStatusCellMargin).constraint
                    
                })
            
            }
            
        }
    
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setupUI(){
    backgroundColor = UIColor(white: 0.97, alpha: 1.0)
    
    addSubview(contentLabel)
    addSubview(statusPictureView)
    
    //添加约束
    contentLabel.snp_makeConstraints { (make) -> Void in
        
        make.leading.equalTo(self).offset(TFStatusCellMargin)
        make.top.equalTo(self).offset(TFStatusCellMargin)
        make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -TFStatusCellMargin)
    }
    
    //添加配图约束
    statusPictureView.snp_makeConstraints { (make) -> Void in
        
        make.top.equalTo(contentLabel.snp_bottom).offset(TFStatusCellMargin)
        make.leading.equalTo(contentLabel)
       
    }
    
    self.snp_makeConstraints { (make) -> Void in
        
      self.bottomConstraint = make.bottom.equalTo(statusPictureView.snp_bottom).offset(TFStatusCellMargin).constraint
    }
        
    }
    
    //MARK: - 懒加载控件
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), textSize: 15)
        
       label.numberOfLines = 0
        return label
    }()

    private lazy var statusPictureView: TFStatusPictureView = TFStatusPictureView()
    
}

